package com.echo.controller;

import com.echo.entity.bean.CommonResult;
import com.echo.entity.bean.PageResult;
import com.echo.entity.dto.*;
import com.echo.entity.vo.TopicListVO;
import com.echo.service.impl.TopicServiceImpl;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/***
 * @date   2021/8/2 0002 15:11
 * @author JackeyTower
 */
@RestController
@RequestMapping("/topic")
public class TopicController {

    private TopicServiceImpl topicService;

    public TopicController(TopicServiceImpl topicService) {
        this.topicService = topicService;
    }

    /**
     * 用户添加话题
     * @return
     */
    @PostMapping("/add")
    public CommonResult<String> addTopic(@RequestBody TopicAddDTO topicAddDTO, HttpServletRequest request){
        Integer uid = (Integer) request.getAttribute("uid");
        if (!uid.equals(topicAddDTO.getUserId())){
            throw new AuthenticationCredentialsNotFoundException("请先登录");
        }

        topicService.addTopic(topicAddDTO);
        return new CommonResult<>(20000,"OK","添加成功");
    }

    /**
     * 用户删除自己创建的话题
     * @param topicDeleteDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public CommonResult<String> deleteTopicByCreator(@RequestBody TopicDeleteDTO topicDeleteDTO,HttpServletRequest request) throws Exception {
        Integer uid = (Integer) request.getAttribute("uid");
        if (!uid.equals(topicDeleteDTO.getUserId())){
            throw new AuthenticationCredentialsNotFoundException("请先登录");
        }

        topicService.deleteTopicByCreator(topicDeleteDTO);
        return new CommonResult<>(20000,"OK","删除成功");
    }

    /**
     * 用户更新自己话题的内容
     * @param topicUpdateDTO
     * @return
     */
    @PostMapping("/update")
    public CommonResult<String> updateTopicByCreator(@RequestBody TopicUpdateDTO topicUpdateDTO,HttpServletRequest request){
        Integer uid = (Integer) request.getAttribute("uid");
        if (!uid.equals(topicUpdateDTO.getUserId())){
            throw new AuthenticationCredentialsNotFoundException("请先登录");
        }

        topicService.updateTopicByCreator(topicUpdateDTO);
        return new CommonResult<>(20000,"OK","更新成功");
    }


    /**
     * 分页获取所有话题列表简略信息（可按时间排序）
     * @return
     */
    @PostMapping("/topic-list")
    public CommonResult<PageResult<TopicListVO>> getAllPaginatedTopicList(@RequestBody PageParamDTO pageParamDTO){
        PageResult<TopicListVO> allPaginatedTopic = topicService.getAllPaginatedTopic(pageParamDTO);
        return new CommonResult<>(20000,"OK",allPaginatedTopic);
    }


    /**
     * 条件查询话题:根据标题查询 根据创建者名查询 根据类型查询 根据时间排序
     * @param topicQueryByParamDTO
     * @return
     */
    @PostMapping("test")
    public CommonResult<PageResult<TopicListVO>> getAllPaginatedTopicListByParam(@RequestBody TopicQueryByParamDTO topicQueryByParamDTO){
        PageResult<TopicListVO> allPaginatedTopicListByParam = topicService.getAllPaginatedTopicListByParam(topicQueryByParamDTO);
        return new CommonResult<>(20000,"OK",allPaginatedTopicListByParam);
    }

    /**
     * 给某话题点赞
     * @param tid 话题id
     * @return
     */
    @GetMapping("/like/{tid}")
    public CommonResult<String> giveLikeForTopic(@PathVariable("tid") String tid){
        topicService.giveLikeForTopic(tid);
        return new CommonResult<>(20000,"OK","点赞成功");
    }

}
